package com.zhl.controller;

import com.zhl.entity.Link;
import com.zhl.entity.ZhlResult;
import com.zhl.service_.LinkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/11/6.
 */
@Controller
public class LinkController {

    Logger logger = LoggerFactory.getLogger("LinkController");

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private LinkService linkService;

    /**
     * 查询所有友人帐
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/api/link")
    @ResponseBody
    public ZhlResult linkList(int pageNumber, int pageSize) {
        logger.info("查询所有友人帐");

        List<Link> links = linkService.linkList(pageNumber,pageSize);
        int totalCount = linkService.totalCount();
        ZhlResult zhlResult = ZhlResult.ok(links);
        zhlResult.setTotalCount(totalCount);
        return zhlResult;

    }


    /**
     * 根据id查询友人帐
     * @param id
     * @return
     */
    @GetMapping("/api/querylinkById/{id}")
    @ResponseBody
    public ZhlResult querylinkById(@PathVariable("id")int id) {
        logger.info("根据id查询友人帐");

        Link link = linkService.querylinkById(id);
        ZhlResult zhlResult = ZhlResult.ok(link);
        return zhlResult;

    }

    /**
     * 增加友人帐
     * @return
     */
    @RequestMapping(value = "/api/addlink", method = RequestMethod.POST)
    @ResponseBody
    public ZhlResult addLink(Link link) {
        logger.info("添加友人帐");
        Date date = new Date();
        String datestr = this.format.format(date);
        link.setCreateTime(datestr);
        int i = linkService.addLink(link);
        if (i==1) {
            return ZhlResult.ok();

        }
        return ZhlResult.build(400,"添加失败");

    }

    /**
     * 修改友人帐
     *
     */
    @RequestMapping(value = "/api/updatelink", method = RequestMethod.POST)
    @ResponseBody
    public ZhlResult updateLink(Link link) {
        logger.info("修改友人帐");

        Link oldlink = linkService.querylinkById(link.getId());
        if (oldlink == null) {
            return ZhlResult.build(400,"该友人帐不存在");
        }

        oldlink.setTitle(link.getTitle());
        oldlink.setSynopsis(link.getSynopsis());
        oldlink.setIcon(link.getIcon());
        oldlink.setUrl(link.getUrl());

        int i = linkService.updateLink(oldlink);

        if (i==1) {
            return ZhlResult.ok();
        }
        return ZhlResult.build(400,"添加失败");

    }


    /**
     * 删除友人帐
     * @param id
     * @return
     */
    @GetMapping("/api/deleteLink/{id}")
    @ResponseBody
    public ZhlResult deleteLink(@PathVariable("id")int id) {
        logger.info("删除友人帐");

        int i = linkService.deleteLink(id);
        if (i == 1 ) {
            ZhlResult zhlResult = ZhlResult.build(400,"删除失败");
            return zhlResult;
        }else {
            ZhlResult zhlResult = ZhlResult.ok();
            return zhlResult;

        }

    }



}
